package com.test.down;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import tv.danmaku.ijk.media.exo.demo.widget.media.IjkVideoView;
import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * @author he xiaohui
 * <p>
 * 2015-9-6 上午10:22:58
 */
@SuppressLint("NewApi")
public class FullVideoActivity extends AppCompatActivity implements View.OnClickListener {
    private IjkVideoView videoview;

    private View title_layout, loading;

    private RelativeLayout video_layout;

    private VideoViewController mycontroller;

    private TextView title_tv;

    private String path;

    private String title;


    public int currentposition;

    public PowerManager.WakeLock mWakeLock;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fullvideoview_layout);
        initViews();
        initListener();
        initDatas();
    }


    protected void initViews() {
        title_layout = (View) findViewById(R.id.title_layout);
        video_layout = findViewById(R.id.video_layout);
        video_layout.getLayoutParams().width = MyApplication.height;
        video_layout.getLayoutParams().height = MyApplication.width;
        title_tv = (TextView) findViewById(R.id.title_tv);
        videoview = (IjkVideoView) findViewById(R.id.videoview);
        mycontroller = (VideoViewController) findViewById(R.id.mycontroller);
        mycontroller.setSurface_view(video_layout, videoview);
        loading = findViewById(R.id.loading);
        mycontroller.hiddenDownLoad(true);
    }


    protected void initListener() {
        mycontroller.onVideoClickListener = new VideoViewController.OnVideoClickListener() {
            @Override
            public void onVideClick(boolean isShow, boolean isAuto) {
                title_layout.setVisibility(isShow ? View.VISIBLE : View.GONE);
            }
        };
    }


    protected void initDatas() {
        path = getIntent().getStringExtra("path");
        title = getIntent().getStringExtra("title");
        Log.d("========",path);
        title_tv.setText(title);
        if (null != path) {
            mycontroller.preparedListener = new IMediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(IMediaPlayer mp) {
                    int w = mp.getVideoWidth();
                    int h = mp.getVideoHeight();
                    if (w < h) {
                        videoview.getLayoutParams().height = MyApplication.width;
                        videoview.getLayoutParams().width = MyApplication.width * w / h;
                    } else {
                        videoview.getLayoutParams().height = MyApplication.width;
                        videoview.getLayoutParams().width = MyApplication.height;
                    }
                }
            };
            mycontroller.completeListener = new VideoViewController.OnCompleteListener() {
                @Override
                public void onCompletion() {
                    releaseWakeLock();
                    onBackPressed();
                }
            };
            mycontroller.play(path);
        }
    }

    private void refreshProgress() {
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, 5000);
    }

    public void releaseWakeLock() {
        if (mWakeLock != null) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (null != mWakeLock && !mycontroller.isPlaying()) {
                        mWakeLock.release();
                        mWakeLock = null;
                    }
                }
            }, 30);

        }
    }

    @SuppressLint("Wakelock")
    @SuppressWarnings("deprecation")
    public void initWakeLock() {
        if (mWakeLock == null) {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "TAG");
            mWakeLock.acquire();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backbtn:
                onBackPressed();
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    showController(false);
                    break;
            }
        }
    };

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    };

    private void showController(boolean isShow) {
        //mycontroller.showController(isShow);
        title_layout.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onDestroy() {
        //ReleaseWakeLock();
        super.onDestroy();
        stopPlay();
    }

    @Override
    public void onBackPressed() {
        stopPlay();
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // ReleaseWakeLock();
        if (null != mycontroller) {
            currentposition = videoview.getCurrentPosition();
            mycontroller.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initWakeLock();
        if (null != mycontroller) {
            mycontroller.restart(currentposition);
            refreshProgress();
        }


    }

    public void ReleaseWakeLock() {
        if (mWakeLock != null) {

            mWakeLock.release();
            mWakeLock = null;
        }


    }

    private void stopPlay() {
        handler.removeCallbacks(runnable);
        if (null != mycontroller) {
            mycontroller.resetPlayer();
        }
    }
}

